home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-07-03 | 4.9 KB | 163 lines | [TEXT/R*ch] |
- (* Substring -- 1995-06-15 *)
-
- local
- prim_val sub_ : string -> int -> char = 2 "get_nth_char";
- prim_val mkstring_ : int -> string = 1 "create_string";
- prim_val blit_ : string -> int -> string -> int -> int -> unit
- = 5 "blit_string";
- in
-
- type substring = string * int * int
- (* Invariant on values (s, i, n) of type substring:
- * 0 <= i <= i+n <= size s,
- * or equivalently, 0 <= i and 0 <= n and i+n <= size s.
- *)
-
- fun base arg = arg
-
- fun string (s, i, n) =
- let val newstr = mkstring_ n
- in blit_ s i newstr 0 n; newstr end;
-
- fun extract (s, i, NONE) =
- if 0 <= i andalso i <= size s then (s, i, size s - i)
- else raise General.Subscript
- | extract (s, i, SOME n) =
- if 0 <= i andalso 0 <= n andalso n <= size s - i then (s, i, n)
- else raise General.Subscript
-
- fun substring (s, i, n) = extract(s, i, SOME n);
-
- fun all s = (s, 0, size s)
-
- fun getc (s, i, 0) = NONE
- | getc (s, i, n) = SOME(sub_ s i, (s, i+1, n-1))
-
- fun first (s, i, n) =
- if n = 0 then NONE else SOME (sub_ s i);
-
- fun isEmpty (s, i, n) = n=0;
-
- fun triml k (s, i, n) =
- if k < 0 then raise Subscript
- else if k > n then (s, i+n, 0)
- else (s, i+k, n-k);
-
- fun trimr k (s, i, n) =
- if k < 0 then raise Subscript
- else if k > n then (s, i, 0)
- else (s, i, n-k);
-
- fun sub((s', i', n'), i) =
- if i<0 orelse i >= n' then raise Subscript
- else sub_ s' (i'+i);
-
- fun size (_, _, n) = n
-
- fun slice ((s', i', n'), i, NONE) =
- if 0 <= i andalso i <= n' then (s', i'+i, n'-i)
- (* If the argument is valid, then so is the result:
- * 0 <= i' <= i'+i <= i'+i + (n'-i) = i'+n' <= size s' *)
- else raise Subscript
- | slice ((s', i', n'), i, SOME n) =
- if 0 <= i andalso 0 <= n andalso i+n <= n' then (s', i'+i, n)
- (* If the argument is valid, then so is the result:
- * 0 <= i' <= i'+i <= i'+i + n <= i'+n' <= size s' *)
- else raise Subscript
-
- fun splitAt ((s, i, n), k) =
- if k < 0 orelse k > n then raise Subscript
- else ((s, i, k), (s, i+k, n-k));
-
- fun concat strs =
- let fun acc [] len = len
- | acc ((_, _, len1)::vr) len = acc vr (len1 + len)
- val len = acc strs 0
- val newstr = if len > String.maxLen then raise Size else mkstring_ len
- fun copyall to [] = () (* Now: to = len *)
- | copyall to ((s1, i1, len1)::vr) =
- (blit_ s1 i1 newstr to len1; copyall (to+len1) vr)
- in copyall 0 strs; newstr end;
-
- fun compare ((s1, i1, n1), (s2, i2, n2)) =
- let val stop = if n1 < n2 then n1 else n2
- fun h j = (* At this point (s1, i1, j) = (s2, i2, j) *)
- if j = stop then if n1 < n2 then LESS
- else if n1 > n2 then GREATER
- else EQUAL
- else
- let val c1 = sub_ s1 (i1+j)
- val c2 = sub_ s2 (i2+j)
- in if c1 < c2 then LESS
- else if c1 > c2 then GREATER
- else h (j+1)
- end
- in h 0 end;
-
- fun collate cmp ((s1, i1, n1), (s2, i2, n2)) =
- let val stop = if n1 < n2 then n1 else n2
- fun h j = (* At this point (s1, i1, j) = (s2, i2, j) *)
- if j = stop then if n1 < n2 then LESS
- else if n1 > n2 then GREATER
- else EQUAL
- else
- case cmp(sub_ s1 (i1+j), sub_ s2 (i2+j)) of
- LESS => LESS
- | GREATER => GREATER
- | EQUAL => h (j+1)
- in h 0 end;
-
- fun foldl f e sus = Strbase.foldl f e sus;
-
- fun foldr f e (s,i,n) =
- let fun h j res = if j<i then res
- else h (j-1) (f (sub_ s j, res))
- in h (i+n-1) e end;
-
- fun explode (s, i, n) =
- let fun h j res = if j<i then res
- else h (j-1) (sub_ s j :: res)
- in h (i+n-1) [] end;
-
- fun app f ss = foldl (fn (x, _) => f x) () ss
-
- local
- open Strbase
- in
- val splitl = splitl
- val splitr = splitr
- val dropl = dropl
- val dropr = dropr
- val takel = takel
- val taker = taker
- val translate = translate
- val tokens = tokens
- val fields = fields
- end
-
- fun position s (s', i, n) =
- let val len = String.size s
- fun eq j k = j >= len orelse
- sub_ s j = sub_ s' k andalso eq (j+1) (k+1)
- val stop = i+n-len
- fun cmp k = if k>stop then (s', i+n, 0) (* failure *)
- else if eq 0 k then (s', k, n-(k-i)) else cmp(k+1)
- in cmp i end;
-
- (* Above, (eq j k) means that (s,j,len-j) = (s',k,len-j),
- so (eq 0 k) implies s = (s', k, len).
- At successful termination, i <= k <= i+n-len, so 0 <= k-i <= n-len,
- and therefore n >= n-(k-i) >= len >= 0. It follows that
- 0 <= k <= k + n-(k-i) = n+i <= size s' (by SS(s', i, n) being valid),
- so the resulting substring is valid.
- *)
-
- fun scanString scan (s, i, n) =
- let fun getc k = if k >= n then NONE
- else SOME (sub_ s (i+k), k+1)
- in case scan {getc=getc} i of
- NONE => NONE
- | SOME (res, _) => SOME res
- end
- end (* local *)
-